package com.ruoyi.web.controller.app.user;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.app.domain.bo.AppDetailBo;
import com.ruoyi.app.domain.vo.AppDetailVo;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.service.IAppDetailService;
import com.ruoyi.app.service.IAppUserService;
import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.LimitType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * APP用户账单明细
 *
 * @author heik
 * @date 2025-08-26
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/detail")
public class AppDetailController extends BaseController {

    private final IAppUserService appUserService;
    private final IAppDetailService iAppDetailService;


    /**
     * 查询APP用户账单明细列表
     */
    @GetMapping("/page")
    public Object page(AppDetailBo bo, PageQuery pageQuery) {
        Long id = getUserId();
        HttpServletRequest request = ServletUtils.getRequest();
        String ip = ServletUtils.getClientIP(request);
        log.info("----------------------------------------当前请求的用户为：{}",id);
        log.info("----------------------------------------当前请求的IP为：{}",ip);
        AppUserVo myUserVo = appUserService.queryById(id);
        if (!StringUtils.equals(myUserVo.getStatus(), Constants.SUCCESS)) {
            return R.fail("账号已被冻结或注销，请联系客服或管理员！");
        }
        bo.setUserId(getUserId());
        TableDataInfo<AppDetailVo> page = iAppDetailService.queryPageList(bo, pageQuery);
        page.getData().forEach(e -> {
            e.setCreateTimeFormat(DateUtil.format(e.getCreateTime(), "yyyy/MM/dd HH:mm:ss"));
        });
        return page;
    }
}
